<?php
/**
* Class GenerateFileUpload into Bo Controller
* @author SillySmart
* @copyright SillySmart
* @package Mvc.Controllers.Bo
* @see Mvc.Controllers.Bo.ControllerProtected
* @see Mvc.Controllers.SiteProtected
* @see Sls.Controllers.Core.SLS_GenericController
* @since 1.0
*
*/
class SLS_BoGenerateFileUpload extends SLS_BoControllerProtected
{
	public function action()
	{
		$user = $this->hasAuthorative();
		$xml = $this->getXML();
		$xml = $this->makeMenu($xml);
		$controllersXML = $this->_generic->getControllersXML();
		$controller = array_shift($controllersXML->getTags("//controllers/controller[@isBo='true']/@name"));		
		$action = array_shift($controllersXML->getTags("//controllers/controller[@isBo='true']/scontrollers/scontroller[@name='FileUpload']"));		
		
		// Check if bo controller already exist
		if (empty($controller))
		{
			$xml->startTag("errors");
			$xml->addFullTag("error","Back-office controller could not be found. Please follow the following link to create it",true);
			$xml->endTag("errors");
			$xml->addFullTag("error_type","rights",true);
		}
		// If action already exist
		else if (!empty($action))
		{
			$xml->startTag("errors");
			$xml->addFullTag("error","Translation action already exists",true);
			$xml->endTag("errors");			
		}
		// Else, let's generate action
		else 
		{
			$langs = $this->_generic->getObjectLang()->getSiteLangs();
		
			$params = array(0 => array("key" 	=> "reload",
					  				   "value" 	=> "true"),
					  		1 => array("key" 	=> "Controller",
					  				   "value" 	=> $controller),
						 	2 => array("key" 	=> "actionName",
					  				   "value" 	=> "FileUpload"),
					  		3 => array("key"	=> "token",
					  					"value"	=> sha1(substr($this->_generic->getSiteConfig("privateKey"), 0, 3).substr($this->_generic->getSiteConfig("privateKey"), strlen($this->_generic->getSiteConfig("privateKey"))-3))),
					  		4 => array("key"	=> "indexes",
					  				   "value"	=> "noindex,nofollow")					  			
						    );
			foreach($langs as $lang)
			{
				$tmpParam = array("key" 	=> $lang."-action",
								  "value" 	=> "FileUpload");
				$tmpTitle = array("key" 	=> $lang."-title",
								  "value" 	=> "FileUpload");
				array_push($params,$tmpParam);
				array_push($params,$tmpTitle);
			}
			file_get_contents($this->_generic->getFullPath("SLS_Bo",
														  "AddAction",
														  $params,
														  true));
			$source = file_get_contents($this->_generic->getPathConfig("actionsControllers").$controller."/FileUpload".".controller.php");
			
			
			$action = 	t(2).'// Check account rights'."\n".
				        t(2).'$right = SLS_BoRights::isAuthorized("custom","",$this->_generic->getActionId());'."\n".
				        t(2).'if ($right == -1)'."\n".
				        	t(3).'$this->_generic->redirect("Bo/Connection.sls");'."\n\n".						
						t(2).'$xml = $this->getXML();'."\n".
				        t(2).'$boActions = SLS_BoRights::getDistinctActions();'."\n".
				        t(2).'$xml->startTag("bo_actions");'."\n".
				        t(2).'for($i=0 ; $i<$count=count($boActions) ; $i++)'."\n".
				        t(2).'{'."\n".
					        t(3).'$xml->startTag("bo_action");'."\n".
					        t(3).'foreach($boActions[$i] as $key => $value)'."\n".
					        	t(4).'$xml->addFullTag($key,$value,true);'."\n".
					        t(3).'$xml->endTag("bo_action");'."\n".
				        t(2).'}'."\n".
				        t(2).'$xml->endTag("bo_actions");'."\n\n".
				        t(2).'$this->saveXML($xml);'."\n";
				      
			$source = substr($source,0,strpos($source,"action()")+8)."\n".
							  t(1).'{'."\n".
							  $action.
							  t(1).'}'."\n".
							  '}'."\n".
							  '?>';
			file_put_contents($this->_generic->getPathConfig("actionsControllers").$controller."/FileUpload.controller.php",$source);
						
			$sourceXsl = 	'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" xmlns="http://www.w3.org/1999/xhtml">'."\n".
							'<xsl:template name="FileUpload">'."\n".
							t(2).'<xsl:call-template name="BoMenu" />'."\n".
							t(2).'<div class="bo_content_center">'."\n".
							t(3).'<h1 class="main">Results for : <span class="light">files</span></h1>'."\n".
							t(3).'<p style="display:block;float:left;width:100%;margin-top:20px;">'."\n".
							t(4).'<script type="text/javascript" src="{concat($sls_url_domain,\'/Public/Scripts/\',\'ckfinder/ckfinder.js\')}"></script>'."\n".
							t(4).'<script type="text/javascript">'."\n".
							t(5).'var finder = new CKFinder();'."\n".
							t(5).'finder.basePath = \'<xsl:value-of select="concat($sls_url_domain,\'/Public/Scripts/\',\'ckeditor/\')" />\';'."\n".
							t(5).'finder.create();'."\n".
							t(4).'</script>'."\n".
							t(3).'</p>'."\n".							
							t(2).'</div>'."\n".
							'</xsl:template>'."\n".
							'</xsl:stylesheet>';
			file_put_contents($this->_generic->getPathConfig("viewsBody").$controller."/FileUpload.xsl",$sourceXsl);
			$this->_generic->goDirectTo("SLS_Bo","Bo");
		}
		
		$xml->addFullTag("url_add_controller",$this->_generic->getFullPath("SLS_Bo","AddController",array(0=>array("key"=>"isBo","value"=>"true"))),true);
		$this->saveXML($xml);
	}
}
?>